package com.isoftstone.project.system.controller;


import com.isoftstone.common.BusinessException;
import com.isoftstone.common.constant.Constants;
import com.isoftstone.common.utils.ExcelUtil;
import com.isoftstone.common.utils.JsonUtil;
import com.isoftstone.framework.aop.OpLogAopController;
import com.isoftstone.framework.controller.BaseController;
import com.isoftstone.framework.domain.Page;
import com.isoftstone.framework.domain.PageData;
import com.isoftstone.framework.domain.Tidings;
import com.isoftstone.project.system.domain.SysDepartment;
import com.isoftstone.project.system.domain.SysUser;
import com.isoftstone.project.system.service.IDepartMentService;
import com.isoftstone.project.system.service.ISysUserService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DepartMentController extends BaseController {
    @Autowired
    IDepartMentService departMentService;
    @Autowired
    ISysUserService userService;

    @RequestMapping("/system/dept/deptlist")
    public ModelAndView getSysDeptlistPage() {
        return new ModelAndView("system/dept/dept_list");
    }

    @RequestMapping("/system/dept/getSysDeptlistPage")
    @ResponseBody
    public Object getSysDeptlistPage(Page page) {
        Tidings<List<SysDepartment>> tidings = new Tidings<List<SysDepartment>>();
        PageData pd = this.getPageData();
        pd.put("deptId",this.getUser().getDeptId());
        page.setPd(pd);
        try {
            List<SysDepartment> deptList = departMentService.getSysDeptlistPage(page);
            tidings.setT(deptList);
            tidings.setTotalResult(page.getTotalResult());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(Constants.ERRORMSG);
        }
        return tidings;
    }

    @RequestMapping("/system/dept/getSysDeptlist")
    @ResponseBody
    public String getSysDeptlist() throws Exception {

        String deptId = this.getUser().getDeptId();
        List<SysDepartment> list = departMentService.getDeptAll(deptId);    //获取所有部门
        String json = JsonUtil.beanToJson(list);
        json = json.replaceAll("deptId", "id").replaceAll("parentId", "pId").replaceAll("deptName", "name");
        return json;
    }


    @RequestMapping("/system/dept/saveDept")
    @ResponseBody
    @OpLogAopController(module = "组织机构", methods = "saveDept", description = "添加部门")
    public Object saveDept(@ModelAttribute("dept") SysDepartment dept) {
        Tidings<String> tidings = new Tidings<String>();
        tidings.setStatus(Constants.SUCCESS);
        tidings.setMsg("保存成功！");
        try {
            SysDepartment department = departMentService.getDeptById(dept.getDeptId());
            if(department == null){
                departMentService.saveDept(dept);
            }else{
                departMentService.updateDept(dept);

            }
        } catch (Exception e) {
            tidings.setStatus(Constants.ERROR);
            tidings.setMsg("保存失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/system/dept/UpdateDept")
    @ResponseBody
    @OpLogAopController(module = "组织机构", methods = "updateDept", description = "修改部门信息")
    public Object updateDept(@ModelAttribute("dept") SysDepartment dept) {
        Tidings<String> tidings = new Tidings<String>();
        try {
            List<SysDepartment> deptName = departMentService.getDeptByName(dept.getDeptName());
            if(!deptName.isEmpty()){
                if(!deptName.get(0).getDeptId().equals(dept.getDeptId())) {
                    tidings.setStatus(Constants.ERROR);
                    tidings.setMsg("部门名不能重复！");
                }else {
                    departMentService.updateDept(dept);
                    tidings.setStatus(Constants.SUCCESS);
                    tidings.setMsg("修改成功！");
                }
            }else{
                departMentService.updateDept(dept);
                tidings.setStatus(Constants.SUCCESS);
                tidings.setMsg("修改成功！");
            }

        } catch (Exception e) {
            tidings.setStatus(Constants.ERROR);
            tidings.setMsg("修改失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/system/dept/deleteDept")
    @ResponseBody
    @OpLogAopController(module = "组织机构", methods = "deleteDept", description = "删除部门")
    public Object deleteDept(String deptId) {
        Tidings<String> tidings = new Tidings<String>();
        Map<String,String> data = new HashMap<String,String>();
        try {
            String result = departMentService.deleteDept(deptId);
            if ("OK".equals(result)) {
                SysDepartment dept = departMentService.getDeptById(deptId);
                data.put("uid",dept.getBranch());
                departMentService.deleteDept1(deptId);
                tidings.setStatus(Constants.SUCCESS);
                tidings.setMsg("删除成功！");
            }else {
                tidings.setStatus(Constants.ERROR);
                tidings.setMsg(result);
            }
        } catch (Exception e) {
            tidings.setStatus(Constants.ERROR);
            tidings.setMsg("删除失败！");
            e.printStackTrace();
        }
        return tidings;
    }

    @RequestMapping("/system/dept/toDeptEdit")
    public ModelAndView toDeptEdit(String deptId) {
        ModelAndView md = new ModelAndView();
        try {
            SysDepartment dept = departMentService.getDeptById(deptId);
            md.addObject("dept", dept);
            List<SysDepartment> list = departMentService.getDeptAll(this.getUser().getDeptId());    //获取所有部门
            String json = JsonUtil.beanToJson(list);
            json = json.replaceAll("deptId", "id").replaceAll("parentId", "pId").replaceAll("deptName", "name");
            md.addObject("zTreeNodes", json);
            md.setViewName("system/dept/dept_edit");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md;
    }

    @RequestMapping("/system/dept/toDeptAdd")
    public ModelAndView toDeptAdd(String deptId) {
        ModelAndView md = new ModelAndView();
        try {
            SysDepartment dept = departMentService.getDeptById(deptId);
            md.addObject("dept", dept);
            List<SysDepartment> list = departMentService.getDeptAll(this.getUser().getDeptId());    //获取所有部门
            String json = JsonUtil.beanToJson(list);
            json = json.replaceAll("deptId", "id").replaceAll("parentId", "pId").replaceAll("deptName", "name");
            md.addObject("zTreeNodes", json);
            md.setViewName("system/dept/dept_add");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md;
    }

    /**
     * 部门多选
     *
     * @return
     */
    @RequestMapping("/system/dept/toDeptTree")
    public ModelAndView toDeptTree() {
        ModelAndView md = new ModelAndView();
        try {
            List<SysDepartment> list = departMentService.getDeptAll(this.getUser().getDeptId());    //获取所有部门
            String json = JsonUtil.beanToJson(list);
            json = json.replaceAll("deptId", "id").replaceAll("parentId", "pId").replaceAll("deptName", "name");
            md.addObject("zTreeNodes", json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        md.setViewName("common/deptztree");
        return md;
    }


    @PostMapping("/system/dept/impDepartmentUrl")
    @ResponseBody
    public Object impDepartmentUrl(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<SysDepartment> util = new ExcelUtil<SysDepartment>(SysDepartment.class);
        List<SysDepartment> departmentList = util.importExcel(file.getInputStream());
        String operName = this.getUser().getUserName();
        String message = departMentService.impDepartment(departmentList, updateSupport, operName);
        return Tidings.success(message);

    }

    @GetMapping("/system/dept/impTempDepartmentUrl")
    @ResponseBody
    public Object impTempDepartmentUrl() throws Exception {
        return Tidings.success("机构表.xls");
    }

}

